import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_path/trapezoid_clipper.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/ticket_plan_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/special_list_controller.dart';

///作业列表页面
class SpecialListWidget extends StatelessWidget {
  SpecialListWidget({super.key});

  final controller = Get.put(SpecialListController());

  //获取条件搜索项
  Widget _getSearchConditionWidget() {
    return Row(
      children: [
        Expanded(
            child: CustomSearchEnter(
          hintText: '请输入关键词、票号',
          text: controller.search,
          onCallBack: (value) {
            controller.search = value;
            controller.refreshPage();
          },
        )),
        AppGaps.hGap8,
        SizedBox(
          width: 80.w,
          child: CustomDropdownSelection(
            hintText: '筛选',
            onCallBack: () {
              controller.screen();
            },
          ),
        ),
        // _getFilter()
      ],
    );
  }

  //获取特殊作业票(小票)内容显示
  Widget _getWorkTicketView(TicketPlanReceiptLists bean) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        //作业票名称显示
        Container(
            padding: EdgeInsets.only(left: 12.w),
            alignment: AlignmentDirectional.topStart,
            decoration: BoxDecoration(
                color: const Color(0xff5C6DFF).withOpacity(0.1),
                borderRadius: BorderRadius.all(Radius.circular(8.w))),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  bean.ticketBigTypeName ?? '',
                  style:
                      TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xff5C6DFF)),
                ),
                ClipPath(
                  clipper: TrapezoidClipper(),
                  child: Container(
                    padding: EdgeInsets.only(left: 10.w),
                    alignment: Alignment.center,
                    width: 74.w,
                    height: 24.w,
                    decoration: BoxDecoration(
                      color: const Color(0xff5C6DFF),
                      borderRadius: BorderRadius.circular(3.r),
                    ),
                    child: Text(
                      '作业票',
                      style: TextStyle(
                          fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffFFFFFF)),
                    ),
                  ),
                ),
              ],
            )),
        AppGaps.vGap10,
        //申请人
        Row(
          children: [
            Text(
              '申请人：',
              style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
            ),
            Text(
              bean.applyUserName ?? '',
              style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
            ),
          ],
        ),
        AppGaps.vGap10,
        Row(
          children: [
            Text(
              '计划开始作业时间：',
              style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
            ),
            const Spacer(),
            Text(
              bean.planStartDate ?? '',
              style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
            ),
          ],
        ),
        AppGaps.vGap10,
        //状态及按钮显示
        Row(
          children: [
            _getState(bean.type ?? 0),
            const Spacer(),
            _getStateButton(bean),
          ],
        )
      ],
    ).clickOnce(() {
      controller.toJobTicket(bean);
    });
  }

  // 获取状态显示
  // [state] 作业状态 0：待开票 1：开票中 2：进行中 3：中断 4：已验收
  Widget _getState(int state) {
    String icon = AppImages.icon_to_be_invoiced;
    switch (state) {
      case 0:
        //待开票
        icon = AppImages.icon_to_be_invoiced;
        break;
      case 1:
        //开票中
        icon = AppImages.icon_invoicing;
        break;
      case 2:
        //进行中
        icon = AppImages.icon_in_progress;
        break;
      case 3:
        //中断
        icon = AppImages.icon_to_interrupt;
        break;
      case 4:
        //已验收
        icon = AppImages.icon_accepted;
        break;
    }
    return SvgPicture.asset(
      icon,
      width: 68.w,
      height: 24.w,
    );
  }

  //获取状态按钮
  Widget _getStateButton(TicketPlanReceiptLists bean) {
    String icon = '';
    switch (bean.type) {
      case 0:
      case 1:
        break;
      case 2:
        icon = AppImages.icon_manage_jobs;
        break;
      case 3:
        icon = AppImages.icon_continue_work;
        break;
      default:
        break;
    }
    // 交接班中不显示按钮
    if (bean.isChange == 1) {
      icon = '';
    }
    if (icon.isEmpty) {
      return Container();
    }
    return SvgPicture.asset(
      icon,
      width: 85.w,
      height: 24.w,
    ).click(() {
      //管理作业或继续作业
      controller.toManagementOperation(bean);
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Container(
            padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 10.w),
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: Column(
              children: [
                _getSearchConditionWidget(),
                AppGaps.vGap14,
                Expanded(
                  child: CustomEasyRefreshList<TicketPlanEntity>(
                    key: controller.globalKey,
                    dataLoad: (pageIndex, pageSize, {String? search}) async {
                      return controller.getPackageDutyData(pageIndex, pageSize);
                    },
                    itemBuilder: (TicketPlanEntity item, int index) {
                      return CustomTitleContentView(
                        title: Row(
                          children: [
                            SvgPicture.asset(
                              AppImages.icon_title,
                              width: 20.w,
                              height: 20.w,
                            ),
                            AppGaps.hGap6,
                            Expanded(
                                child: RichText(
                              text: TextSpan(
                                children: [
                                  TextSpan(
                                    text: item.ticketName,
                                    style: TextStyle(
                                        color: const Color(0xff333333),
                                        height: 1.2,
                                        fontWeight: FontWeight.w500,
                                        fontSize: 15.sp),
                                  ),
                                  // TextSpan(
                                  //   text: '（${item.ticketNo}）',
                                  //   style: TextStyle(
                                  //       color: const Color(0xff666666),
                                  //       height: 1.2,
                                  //       fontWeight: FontWeight.w500,
                                  //       fontSize: 12.sp),
                                  // ),
                                ],
                              ),
                            )),
                            SvgPicture.asset(
                              AppImages.icon_details_right,
                              width: 14.w,
                              height: 14.w,
                            ),
                          ],
                        ).clickOnce(() {
                          controller.toJobDetails(item.id, item.identityUserId);
                        }),
                        content: ListView.separated(
                          shrinkWrap: true,
                          //为true可以解决子控件必须设置高度的问题
                          physics: const NeverScrollableScrollPhysics(),
                          //禁用滑动事件
                          itemCount: item.receiptLists!.length,
                          // padding: EdgeInsets.only(left: 10.w),
                          separatorBuilder: (BuildContext context, int index) {
                            return Padding(
                                padding: EdgeInsets.symmetric(vertical: 16.w),
                                child: const CustomGradientLine());
                          },
                          itemBuilder: (BuildContext context, int index) {
                            return _getWorkTicketView(item.receiptLists![index]);
                          },
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
          floatingActionButton: SvgPicture.asset(
            AppImages.icon_list_add,
          ).clickOnce(controller.addJobPlanRequest),
        );
      },
    );
  }
}
